package cn.myjerry.collection;

import java.util.LinkedList;

/**
 * 
 * @author Jerry
 *
 */

public class SxtMap002 {

	LinkedList[] arr = new LinkedList[999];
	int size;
	
	public int size() {
		return size;
	}
	
	public void put(Object key, Object value) {
		SxtEntry e = new SxtEntry(key, value);
		int hash = key.hashCode();
		hash = hash < 0 ? -hash : hash;
		int a = hash % arr.length;
		if(arr[a] == null) {
			LinkedList<SxtEntry> list = new LinkedList<SxtEntry>();
			list.add(e);
			arr[a] = list;
		} else {
			LinkedList<?> list = arr[a];
			for(int i=0;i<list.size();i++) {
				SxtEntry e2 = (SxtEntry) list.get(i);
				if(e2.getKey().equals(key)) {
					e2.setValue(value);
					return;
				}
			}
			arr[a].add(e);
		}
		size++;
	}
	
	public Object get(Object key) {
		int hash = key.hashCode();
		hash = hash < 0 ? -hash : hash;
		int a = hash % arr.length;
		if(arr[a] != null ) {
			LinkedList<?> list = arr[a];
			for(int i=0;i<list.size();i++) {
				SxtEntry e = (SxtEntry) list.get(i);
				if(e.getKey().equals(key)) {
					return e.getValue();
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		SxtMap002 map = new SxtMap002();
		map.put("aaa", "AAA");
		map.put("aaa", "BBB");
		map.put("ccc", "CCC");
		map.put("ddd", "DDD");
		System.out.println(map.get("ccc"));
		System.out.println(map.size());
	}

}
